﻿using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using PointOfSale.Report.Sale;
using PointOfSale.Global;
using PointOfSale.Class;
using PointOfSale.PointOfSale;

namespace PointOfSale.Transaction
{
	public class frmEditSaleBill : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.Label label15;
		private System.Windows.Forms.Label label16;
		private System.Windows.Forms.LookupComboBox cboName;
		private System.Windows.Forms.LookupComboBox cboUnit;
		private System.Windows.Forms.TextBox txtQty;
		private System.Windows.Forms.TextBox txtAmt;
		private System.Windows.Forms.Label label21;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Button btnAdd;
		private System.Windows.Forms.Button btnDelete;
		private System.Windows.Forms.Button btnUpdate;
		private System.Windows.Forms.Button btnSaveAndPrint;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.DateTimePicker dtpSale;
		public System.Windows.Forms.ListView lsvSale;
		private System.Windows.Forms.ColumnHeader clhName;
		private System.Windows.Forms.ColumnHeader clhQty;
		private System.Windows.Forms.ColumnHeader clhUnit;
		private System.Windows.Forms.ColumnHeader clhRate;
		private System.Windows.Forms.ColumnHeader clhAmount;
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.CheckBox chbIsFinished;
		private Label lblFactor;
		private TextBox txtSubUnit;
		private System.Windows.Forms.GroupBox groupBox6;
		private System.Windows.Forms.CheckBox chkBoxPostInfo;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		public System.Windows.Forms.LookupComboBox cboGuestName;
		private System.Windows.Forms.TextBox txtTotalAmt;
		private System.Windows.Forms.GroupBox groupBox7;
		private System.Windows.Forms.RichTextBox txtRemark;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txtTotal;
		private System.Windows.Forms.TextBox txtRoomService;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox txtRate;
		private System.Windows.Forms.LookupComboBox cboLoaiKhach;
		private System.Windows.Forms.ColumnHeader colItemId;
		private System.Windows.Forms.ColumnHeader colUnitId;
		
		public static bool isGuestLoading = false;
		private System.Windows.Forms.ComboBox cboSoPhong;
		private System.Windows.Forms.ComboBox cboLoaiDichVu;
		private System.Windows.Forms.Label lblLoaiDichVu;
		private String roomNumber;
		private int saleId = 0;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.RadioButton rdbVND;
		private System.Windows.Forms.RadioButton rdbUSD;
		private System.Windows.Forms.Label lblMaMatHang;
		private System.Windows.Forms.TextBox txtMaMatHang;
		private System.Windows.Forms.TextBox txtSoHoaDonCon;
		private System.Windows.Forms.Label lblSoHoaDonCon;
		private System.Windows.Forms.ColumnHeader colSoHoaDonCon;
		private DataTable dtChiTietHoaDonCu = null;
		
		public frmEditSaleBill(int si)
		{
			InitializeComponent();
			this.saleId = si;
		}
		
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.lblLoaiDichVu = new System.Windows.Forms.Label();
			this.cboLoaiDichVu = new System.Windows.Forms.ComboBox();
			this.groupBox6 = new System.Windows.Forms.GroupBox();
			this.cboSoPhong = new System.Windows.Forms.ComboBox();
			this.chkBoxPostInfo = new System.Windows.Forms.CheckBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.cboGuestName = new System.Windows.Forms.LookupComboBox();
			this.cboLoaiKhach = new System.Windows.Forms.LookupComboBox();
			this.label5 = new System.Windows.Forms.Label();
			this.dtpSale = new System.Windows.Forms.DateTimePicker();
			this.label3 = new System.Windows.Forms.Label();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.lblSoHoaDonCon = new System.Windows.Forms.Label();
			this.txtSoHoaDonCon = new System.Windows.Forms.TextBox();
			this.txtMaMatHang = new System.Windows.Forms.TextBox();
			this.lblMaMatHang = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.rdbUSD = new System.Windows.Forms.RadioButton();
			this.rdbVND = new System.Windows.Forms.RadioButton();
			this.txtRate = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.txtRoomService = new System.Windows.Forms.TextBox();
			this.txtTotal = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.groupBox7 = new System.Windows.Forms.GroupBox();
			this.txtRemark = new System.Windows.Forms.RichTextBox();
			this.txtTotalAmt = new System.Windows.Forms.TextBox();
			this.lsvSale = new System.Windows.Forms.ListView();
			this.colSoHoaDonCon = new System.Windows.Forms.ColumnHeader();
			this.clhName = new System.Windows.Forms.ColumnHeader();
			this.colItemId = new System.Windows.Forms.ColumnHeader();
			this.clhQty = new System.Windows.Forms.ColumnHeader();
			this.clhUnit = new System.Windows.Forms.ColumnHeader();
			this.colUnitId = new System.Windows.Forms.ColumnHeader();
			this.clhRate = new System.Windows.Forms.ColumnHeader();
			this.clhAmount = new System.Windows.Forms.ColumnHeader();
			this.btnAdd = new System.Windows.Forms.Button();
			this.txtAmt = new System.Windows.Forms.TextBox();
			this.label16 = new System.Windows.Forms.Label();
			this.label15 = new System.Windows.Forms.Label();
			this.label14 = new System.Windows.Forms.Label();
			this.txtQty = new System.Windows.Forms.TextBox();
			this.label13 = new System.Windows.Forms.Label();
			this.label12 = new System.Windows.Forms.Label();
			this.cboUnit = new System.Windows.Forms.LookupComboBox();
			this.cboName = new System.Windows.Forms.LookupComboBox();
			this.btnDelete = new System.Windows.Forms.Button();
			this.btnUpdate = new System.Windows.Forms.Button();
			this.label21 = new System.Windows.Forms.Label();
			this.chbIsFinished = new System.Windows.Forms.CheckBox();
			this.btnSaveAndPrint = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnClose = new System.Windows.Forms.Button();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.groupBox1.SuspendLayout();
			this.groupBox6.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox7.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.lblLoaiDichVu);
			this.groupBox1.Controls.Add(this.cboLoaiDichVu);
			this.groupBox1.Controls.Add(this.groupBox6);
			this.groupBox1.Controls.Add(this.cboLoaiKhach);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.dtpSale);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(680, 80);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Thông tin xuất";
			// 
			// lblLoaiDichVu
			// 
			this.lblLoaiDichVu.AutoSize = true;
			this.lblLoaiDichVu.Location = new System.Drawing.Point(280, 16);
			this.lblLoaiDichVu.Name = "lblLoaiDichVu";
			this.lblLoaiDichVu.Size = new System.Drawing.Size(64, 16);
			this.lblLoaiDichVu.TabIndex = 11;
			this.lblLoaiDichVu.Text = "Loại dịch vụ";
			// 
			// cboLoaiDichVu
			// 
			this.cboLoaiDichVu.Location = new System.Drawing.Point(280, 40);
			this.cboLoaiDichVu.Name = "cboLoaiDichVu";
			this.cboLoaiDichVu.Size = new System.Drawing.Size(104, 21);
			this.cboLoaiDichVu.TabIndex = 10;
			this.cboLoaiDichVu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboLoaiDichVu_KeyDown);
			// 
			// groupBox6
			// 
			this.groupBox6.Controls.Add(this.cboSoPhong);
			this.groupBox6.Controls.Add(this.chkBoxPostInfo);
			this.groupBox6.Controls.Add(this.label2);
			this.groupBox6.Controls.Add(this.label1);
			this.groupBox6.Controls.Add(this.cboGuestName);
			this.groupBox6.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox6.Location = new System.Drawing.Point(392, 8);
			this.groupBox6.Name = "groupBox6";
			this.groupBox6.Size = new System.Drawing.Size(280, 64);
			this.groupBox6.TabIndex = 9;
			this.groupBox6.TabStop = false;
			// 
			// cboSoPhong
			// 
			this.cboSoPhong.Enabled = false;
			this.cboSoPhong.Location = new System.Drawing.Point(24, 32);
			this.cboSoPhong.Name = "cboSoPhong";
			this.cboSoPhong.Size = new System.Drawing.Size(80, 21);
			this.cboSoPhong.TabIndex = 17;
			this.cboSoPhong.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboSoPhong_KeyDown);
			this.cboSoPhong.SelectedIndexChanged += new System.EventHandler(this.cboSoPhong_SelectedIndexChanged);
			// 
			// chkBoxPostInfo
			// 
			this.chkBoxPostInfo.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.chkBoxPostInfo.Location = new System.Drawing.Point(16, 0);
			this.chkBoxPostInfo.Name = "chkBoxPostInfo";
			this.chkBoxPostInfo.Size = new System.Drawing.Size(150, 16);
			this.chkBoxPostInfo.TabIndex = 14;
			this.chkBoxPostInfo.Text = "Thông tin khác";
			this.chkBoxPostInfo.KeyDown += new System.Windows.Forms.KeyEventHandler(this.chkBoxPostInfo_KeyDown);
			this.chkBoxPostInfo.CheckedChanged += new System.EventHandler(this.chkBoxPostInfo_CheckedChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(24, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 16);
			this.label2.TabIndex = 6;
			this.label2.Text = "Số phòng";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(112, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 16);
			this.label1.TabIndex = 5;
			this.label1.Text = "Tên khách";
			// 
			// cboGuestName
			// 
			this.cboGuestName.AllowTypeAllSymbols = true;
			this.cboGuestName.Enabled = false;
			this.cboGuestName.Location = new System.Drawing.Point(112, 32);
			this.cboGuestName.Name = "cboGuestName";
			this.cboGuestName.Size = new System.Drawing.Size(160, 21);
			this.cboGuestName.TabIndex = 16;
			this.cboGuestName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboGuestName_KeyDown);
			// 
			// cboLoaiKhach
			// 
			this.cboLoaiKhach.AllowTypeAllSymbols = true;
			this.cboLoaiKhach.Location = new System.Drawing.Point(144, 40);
			this.cboLoaiKhach.Name = "cboLoaiKhach";
			this.cboLoaiKhach.Size = new System.Drawing.Size(128, 21);
			this.cboLoaiKhach.TabIndex = 8;
			this.cboLoaiKhach.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboLoaiKhach_KeyDown);
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(144, 24);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(120, 16);
			this.label5.TabIndex = 7;
			this.label5.Text = "Loại khách";
			// 
			// dtpSale
			// 
			this.dtpSale.CustomFormat = "dd/MM/yyyy HH:mm";
			this.dtpSale.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
			this.dtpSale.Location = new System.Drawing.Point(16, 40);
			this.dtpSale.Name = "dtpSale";
			this.dtpSale.Size = new System.Drawing.Size(120, 20);
			this.dtpSale.TabIndex = 1;
			this.dtpSale.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dtpSale_KeyDown);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 24);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(32, 16);
			this.label3.TabIndex = 3;
			this.label3.Text = "Ngày lập";
			// 
			// groupBox4
			// 
			this.groupBox4.BackColor = System.Drawing.SystemColors.ControlLight;
			this.groupBox4.Controls.Add(this.lblSoHoaDonCon);
			this.groupBox4.Controls.Add(this.txtSoHoaDonCon);
			this.groupBox4.Controls.Add(this.txtMaMatHang);
			this.groupBox4.Controls.Add(this.lblMaMatHang);
			this.groupBox4.Controls.Add(this.groupBox2);
			this.groupBox4.Controls.Add(this.txtRate);
			this.groupBox4.Controls.Add(this.label6);
			this.groupBox4.Controls.Add(this.txtRoomService);
			this.groupBox4.Controls.Add(this.txtTotal);
			this.groupBox4.Controls.Add(this.label4);
			this.groupBox4.Controls.Add(this.groupBox7);
			this.groupBox4.Controls.Add(this.txtTotalAmt);
			this.groupBox4.Controls.Add(this.lsvSale);
			this.groupBox4.Controls.Add(this.btnAdd);
			this.groupBox4.Controls.Add(this.txtAmt);
			this.groupBox4.Controls.Add(this.label16);
			this.groupBox4.Controls.Add(this.label15);
			this.groupBox4.Controls.Add(this.label14);
			this.groupBox4.Controls.Add(this.txtQty);
			this.groupBox4.Controls.Add(this.label13);
			this.groupBox4.Controls.Add(this.label12);
			this.groupBox4.Controls.Add(this.cboUnit);
			this.groupBox4.Controls.Add(this.cboName);
			this.groupBox4.Controls.Add(this.btnDelete);
			this.groupBox4.Controls.Add(this.btnUpdate);
			this.groupBox4.Controls.Add(this.label21);
			this.groupBox4.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox4.Location = new System.Drawing.Point(8, 96);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(672, 304);
			this.groupBox4.TabIndex = 3;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "Thông tin mặt hàng";
			// 
			// lblSoHoaDonCon
			// 
			this.lblSoHoaDonCon.AutoSize = true;
			this.lblSoHoaDonCon.Location = new System.Drawing.Point(8, 16);
			this.lblSoHoaDonCon.Name = "lblSoHoaDonCon";
			this.lblSoHoaDonCon.Size = new System.Drawing.Size(59, 16);
			this.lblSoHoaDonCon.TabIndex = 35;
			this.lblSoHoaDonCon.Text = "Số HĐ con";
			// 
			// txtSoHoaDonCon
			// 
			this.txtSoHoaDonCon.Location = new System.Drawing.Point(8, 32);
			this.txtSoHoaDonCon.Name = "txtSoHoaDonCon";
			this.txtSoHoaDonCon.Size = new System.Drawing.Size(64, 20);
			this.txtSoHoaDonCon.TabIndex = 34;
			this.txtSoHoaDonCon.Text = "";
			this.txtSoHoaDonCon.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtSoHoaDonCon_KeyDown);
			// 
			// txtMaMatHang
			// 
			this.txtMaMatHang.Location = new System.Drawing.Point(80, 32);
			this.txtMaMatHang.Name = "txtMaMatHang";
			this.txtMaMatHang.Size = new System.Drawing.Size(64, 20);
			this.txtMaMatHang.TabIndex = 33;
			this.txtMaMatHang.Text = "";
			this.txtMaMatHang.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtMaMatHang_KeyDown);
			// 
			// lblMaMatHang
			// 
			this.lblMaMatHang.AutoSize = true;
			this.lblMaMatHang.Location = new System.Drawing.Point(80, 16);
			this.lblMaMatHang.Name = "lblMaMatHang";
			this.lblMaMatHang.Size = new System.Drawing.Size(70, 16);
			this.lblMaMatHang.TabIndex = 32;
			this.lblMaMatHang.Text = "Mã mặt hàng";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.rdbUSD);
			this.groupBox2.Controls.Add(this.rdbVND);
			this.groupBox2.Location = new System.Drawing.Point(288, 232);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(80, 64);
			this.groupBox2.TabIndex = 31;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Trả bằng";
			// 
			// rdbUSD
			// 
			this.rdbUSD.Location = new System.Drawing.Point(8, 32);
			this.rdbUSD.Name = "rdbUSD";
			this.rdbUSD.Size = new System.Drawing.Size(64, 24);
			this.rdbUSD.TabIndex = 1;
			this.rdbUSD.Text = "USD";
			this.rdbUSD.CheckedChanged += new System.EventHandler(this.rdbUSD_CheckedChanged);
			// 
			// rdbVND
			// 
			this.rdbVND.Checked = true;
			this.rdbVND.Location = new System.Drawing.Point(8, 16);
			this.rdbVND.Name = "rdbVND";
			this.rdbVND.Size = new System.Drawing.Size(64, 16);
			this.rdbVND.TabIndex = 0;
			this.rdbVND.TabStop = true;
			this.rdbVND.Text = "VNĐ";
			// 
			// txtRate
			// 
			this.txtRate.Location = new System.Drawing.Point(424, 32);
			this.txtRate.Name = "txtRate";
			this.txtRate.Size = new System.Drawing.Size(80, 20);
			this.txtRate.TabIndex = 30;
			this.txtRate.Text = "";
			this.txtRate.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtRate_KeyDown);
			this.txtRate.TextChanged += new System.EventHandler(this.txtRate_TextChanged);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(376, 265);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(96, 14);
			this.label6.TabIndex = 29;
			this.label6.Text = "Các loại phí khác";
			// 
			// txtRoomService
			// 
			this.txtRoomService.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtRoomService.Location = new System.Drawing.Point(480, 259);
			this.txtRoomService.Name = "txtRoomService";
			this.txtRoomService.Size = new System.Drawing.Size(96, 20);
			this.txtRoomService.TabIndex = 28;
			this.txtRoomService.Text = "0.0";
			this.txtRoomService.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.txtRoomService.TextChanged += new System.EventHandler(this.txtRoomService_TextChanged);
			// 
			// txtTotal
			// 
			this.txtTotal.BackColor = System.Drawing.SystemColors.Menu;
			this.txtTotal.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtTotal.Location = new System.Drawing.Point(480, 280);
			this.txtTotal.Name = "txtTotal";
			this.txtTotal.ReadOnly = true;
			this.txtTotal.Size = new System.Drawing.Size(96, 20);
			this.txtTotal.TabIndex = 27;
			this.txtTotal.Text = "0.0";
			this.txtTotal.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(376, 283);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(88, 16);
			this.label4.TabIndex = 26;
			this.label4.Text = "Tổng hóa đơn";
			// 
			// groupBox7
			// 
			this.groupBox7.Controls.Add(this.txtRemark);
			this.groupBox7.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox7.Location = new System.Drawing.Point(8, 232);
			this.groupBox7.Name = "groupBox7";
			this.groupBox7.Size = new System.Drawing.Size(272, 64);
			this.groupBox7.TabIndex = 23;
			this.groupBox7.TabStop = false;
			this.groupBox7.Text = "Ghi chú";
			// 
			// txtRemark
			// 
			this.txtRemark.Location = new System.Drawing.Point(8, 16);
			this.txtRemark.Name = "txtRemark";
			this.txtRemark.Size = new System.Drawing.Size(256, 40);
			this.txtRemark.TabIndex = 0;
			this.txtRemark.Text = "";
			// 
			// txtTotalAmt
			// 
			this.txtTotalAmt.BackColor = System.Drawing.SystemColors.Menu;
			this.txtTotalAmt.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtTotalAmt.Location = new System.Drawing.Point(480, 238);
			this.txtTotalAmt.Name = "txtTotalAmt";
			this.txtTotalAmt.ReadOnly = true;
			this.txtTotalAmt.Size = new System.Drawing.Size(96, 20);
			this.txtTotalAmt.TabIndex = 22;
			this.txtTotalAmt.Text = "0.0";
			this.txtTotalAmt.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.txtTotalAmt.TextChanged += new System.EventHandler(this.txtTotalAmt_TextChanged);
			// 
			// lsvSale
			// 
			this.lsvSale.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					  this.colSoHoaDonCon,
																					  this.clhName,
																					  this.colItemId,
																					  this.clhQty,
																					  this.clhUnit,
																					  this.colUnitId,
																					  this.clhRate,
																					  this.clhAmount});
			this.lsvSale.FullRowSelect = true;
			this.lsvSale.GridLines = true;
			this.lsvSale.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.lsvSale.HideSelection = false;
			this.lsvSale.Location = new System.Drawing.Point(8, 56);
			this.lsvSale.MultiSelect = false;
			this.lsvSale.Name = "lsvSale";
			this.lsvSale.Size = new System.Drawing.Size(584, 176);
			this.lsvSale.TabIndex = 17;
			this.lsvSale.View = System.Windows.Forms.View.Details;
			this.lsvSale.SelectedIndexChanged += new System.EventHandler(this.lsvSale_SelectedIndexChanged);
			// 
			// colSoHoaDonCon
			// 
			this.colSoHoaDonCon.Text = "Số HĐ con";
			this.colSoHoaDonCon.Width = 68;
			// 
			// clhName
			// 
			this.clhName.Text = "Tên hàng";
			this.clhName.Width = 230;
			// 
			// colItemId
			// 
			this.colItemId.Text = "ItemId";
			this.colItemId.Width = 0;
			// 
			// clhQty
			// 
			this.clhQty.Text = "Số lượng";
			this.clhQty.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.clhQty.Width = 59;
			// 
			// clhUnit
			// 
			this.clhUnit.Text = "Đơn vị";
			this.clhUnit.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.clhUnit.Width = 67;
			// 
			// colUnitId
			// 
			this.colUnitId.Text = "UnitId";
			this.colUnitId.Width = 0;
			// 
			// clhRate
			// 
			this.clhRate.Text = "Giá thành";
			this.clhRate.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.clhRate.Width = 78;
			// 
			// clhAmount
			// 
			this.clhAmount.Text = "Thành tiền";
			this.clhAmount.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.clhAmount.Width = 78;
			// 
			// btnAdd
			// 
			this.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnAdd.Location = new System.Drawing.Point(600, 56);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.Size = new System.Drawing.Size(64, 24);
			this.btnAdd.TabIndex = 9;
			this.btnAdd.Text = "Thêm";
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			// 
			// txtAmt
			// 
			this.txtAmt.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
			this.txtAmt.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtAmt.Location = new System.Drawing.Point(512, 32);
			this.txtAmt.Name = "txtAmt";
			this.txtAmt.ReadOnly = true;
			this.txtAmt.Size = new System.Drawing.Size(80, 20);
			this.txtAmt.TabIndex = 12;
			this.txtAmt.Text = "0.0";
			this.txtAmt.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label16
			// 
			this.label16.AutoSize = true;
			this.label16.Location = new System.Drawing.Point(512, 16);
			this.label16.Name = "label16";
			this.label16.Size = new System.Drawing.Size(58, 16);
			this.label16.TabIndex = 13;
			this.label16.Text = "Thành tiền";
			// 
			// label15
			// 
			this.label15.AutoSize = true;
			this.label15.Location = new System.Drawing.Point(424, 16);
			this.label15.Name = "label15";
			this.label15.Size = new System.Drawing.Size(22, 16);
			this.label15.TabIndex = 11;
			this.label15.Text = "Giá";
			// 
			// label14
			// 
			this.label14.AutoSize = true;
			this.label14.Location = new System.Drawing.Point(352, 16);
			this.label14.Name = "label14";
			this.label14.Size = new System.Drawing.Size(36, 16);
			this.label14.TabIndex = 10;
			this.label14.Text = "Đơn vị";
			// 
			// txtQty
			// 
			this.txtQty.BackColor = System.Drawing.SystemColors.Window;
			this.txtQty.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtQty.Location = new System.Drawing.Point(304, 32);
			this.txtQty.Name = "txtQty";
			this.txtQty.Size = new System.Drawing.Size(40, 20);
			this.txtQty.TabIndex = 6;
			this.txtQty.Text = "";
			this.txtQty.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.txtQty.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtQty_KeyDown);
			this.txtQty.TextChanged += new System.EventHandler(this.txtQty_TextChanged);
			// 
			// label13
			// 
			this.label13.AutoSize = true;
			this.label13.Location = new System.Drawing.Point(304, 16);
			this.label13.Name = "label13";
			this.label13.Size = new System.Drawing.Size(49, 16);
			this.label13.TabIndex = 8;
			this.label13.Text = "Số lượng";
			// 
			// label12
			// 
			this.label12.AutoSize = true;
			this.label12.Location = new System.Drawing.Point(152, 16);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(74, 16);
			this.label12.TabIndex = 7;
			this.label12.Text = "Tên mặt hàng";
			// 
			// cboUnit
			// 
			this.cboUnit.AllowTypeAllSymbols = true;
			this.cboUnit.BackColor = System.Drawing.Color.White;
			this.cboUnit.Location = new System.Drawing.Point(352, 32);
			this.cboUnit.Name = "cboUnit";
			this.cboUnit.Size = new System.Drawing.Size(64, 21);
			this.cboUnit.TabIndex = 7;
			this.cboUnit.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboUnit_KeyDown);
			this.cboUnit.SelectedIndexChanged += new System.EventHandler(this.cboUnit_SelectedIndexChanged);
			// 
			// cboName
			// 
			this.cboName.AllowTypeAllSymbols = true;
			this.cboName.BackColor = System.Drawing.SystemColors.Window;
			this.cboName.Location = new System.Drawing.Point(152, 32);
			this.cboName.Name = "cboName";
			this.cboName.Size = new System.Drawing.Size(144, 21);
			this.cboName.TabIndex = 5;
			this.cboName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboName_KeyDown);
			this.cboName.SelectedIndexChanged += new System.EventHandler(this.cboName_SelectedIndexChanged);
			// 
			// btnDelete
			// 
			this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnDelete.Location = new System.Drawing.Point(600, 205);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new System.Drawing.Size(64, 24);
			this.btnDelete.TabIndex = 2;
			this.btnDelete.Text = "Xóa";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnUpdate
			// 
			this.btnUpdate.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnUpdate.Location = new System.Drawing.Point(600, 175);
			this.btnUpdate.Name = "btnUpdate";
			this.btnUpdate.Size = new System.Drawing.Size(64, 23);
			this.btnUpdate.TabIndex = 1;
			this.btnUpdate.Text = "Sửa";
			this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
			// 
			// label21
			// 
			this.label21.Location = new System.Drawing.Point(376, 246);
			this.label21.Name = "label21";
			this.label21.Size = new System.Drawing.Size(72, 16);
			this.label21.TabIndex = 10;
			this.label21.Text = "Tổng số tiền";
			// 
			// chbIsFinished
			// 
			this.chbIsFinished.Location = new System.Drawing.Point(304, 408);
			this.chbIsFinished.Name = "chbIsFinished";
			this.chbIsFinished.Size = new System.Drawing.Size(96, 16);
			this.chbIsFinished.TabIndex = 10;
			this.chbIsFinished.Text = "Đã thanh toán";
			// 
			// btnSaveAndPrint
			// 
			this.btnSaveAndPrint.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnSaveAndPrint.Location = new System.Drawing.Point(416, 408);
			this.btnSaveAndPrint.Name = "btnSaveAndPrint";
			this.btnSaveAndPrint.Size = new System.Drawing.Size(88, 23);
			this.btnSaveAndPrint.TabIndex = 7;
			this.btnSaveAndPrint.Text = "Sửa_In hóa đơn";
			this.btnSaveAndPrint.Click += new System.EventHandler(this.btnSaveAndPrint_Click);
			// 
			// btnSave
			// 
			this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnSave.Location = new System.Drawing.Point(520, 408);
			this.btnSave.Name = "btnSave";
			this.btnSave.TabIndex = 8;
			this.btnSave.Text = "Sửa hóa đơn";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnClose
			// 
			this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnClose.Location = new System.Drawing.Point(608, 408);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(72, 24);
			this.btnClose.TabIndex = 9;
			this.btnClose.Text = "Thoát";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(0, 0);
			this.textBox2.Name = "textBox2";
			this.textBox2.TabIndex = 0;
			this.textBox2.Text = "textBox2";
			// 
			// frmEditSaleBill
			// 
			this.AccessibleRole = System.Windows.Forms.AccessibleRole.Cursor;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(690, 440);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.btnSaveAndPrint);
			this.Controls.Add(this.groupBox4);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.chbIsFinished);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmEditSaleBill";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Chỉnh sửa hóa đơn bán hàng";
			this.TopMost = true;
			this.Load += new System.EventHandler(this.frmPOSTransaction_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox6.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox7.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			if(clsGlobal.g_DepartmentId  == 1)
			{
				//cap nhat lai so luong hang hoa trong kho nhu cu
				for(int i = 0; i < this.dtChiTietHoaDonCu.Rows.Count; i++)
				{
					int itemId = Convert.ToInt32(this.dtChiTietHoaDonCu.Rows[i]["itemId"]);
//					if(!clsItem.HangPhaTron(itemId))
//					{
//						double qty = Convert.ToDouble(this.dtChiTietHoaDonCu.Rows[i]["qty"]);
//						int unitId = Convert.ToInt32(this.dtChiTietHoaDonCu.Rows[i]["unitId"]);
//						int donViChuanId = clsItem.GetDonViChuanId(itemId);
//						double factor = clsUnitConversion.GetFactor(itemId, donViChuanId, unitId);
//						double soLuongTheoDonViChuan = 0.0;
//						if(factor == -1)
//						{
//							MessageBox.Show("Mặt hàng có id " + itemId + " thiếu đơn vị chuyển đổi. "); 
//						}
//						else
//						{
//							soLuongTheoDonViChuan = qty / factor;	
//						}
//						clsStock.UpdateQuantity(itemId, clsGlobal.g_DepartmentId, soLuongTheoDonViChuan, "-");
//					}
//					else
//					{
//						DataTable danhSachThanhPhan = clsStaple.DanhSachThanhPhan(itemId);
//						double soLuongHangTongHop = Convert.ToDouble(this.dtChiTietHoaDonCu.Rows[i]["qty"]);
//						for(int j = 0; j < danhSachThanhPhan.Rows.Count; j++)
//						{
//							int thanhPhanId = Convert.ToInt32(danhSachThanhPhan.Rows[j]["stapleId"]);
//							double soLuongThanhPhanTrongCongThuc = Convert.ToDouble(danhSachThanhPhan.Rows[j]["qty"]);
//							int donViChuanIdCuaThanhPhan = clsItem.GetDonViChuanId(thanhPhanId);
//							int donViIdCuaThanhPhanTrongCongThuc = Convert.ToInt32(danhSachThanhPhan.Rows[j]["unitId"]);
//							double factor = clsUnitConversion.GetFactor(thanhPhanId, donViChuanIdCuaThanhPhan, donViIdCuaThanhPhanTrongCongThuc);
//							double soLuongThanhPhanTheoDonViChuan = soLuongHangTongHop * soLuongThanhPhanTrongCongThuc / factor;
//							clsStock.UpdateQuantity(thanhPhanId, clsGlobal.g_DepartmentId, soLuongThanhPhanTheoDonViChuan, "-");
//						}
//					}
				}
			}
			this.Close();
		}

		private void btnSaveAndPrint_Click(object sender, System.EventArgs e)
		{
			//Lap hoa don trong csdl
			double dichVu = Convert.ToDouble(this.txtRoomService.Text.Trim());
			double tongSo = Convert.ToDouble(this.txtTotal.Text.Trim());
			btnSave_Click(sender, e);

			frmInvoice _frmInvoice = new frmInvoice(this.saleId);
			_frmInvoice.Address = "Địa chỉ: " + clsGlobal.g_Hashtable["CompanyAddress"].ToString();
			_frmInvoice.Phone = "Điện thoại: " + clsGlobal.g_Hashtable["CompanyTel"].ToString();
			_frmInvoice.Fax = "Fax: " + clsGlobal.g_Hashtable["CompanyFax"].ToString();
			_frmInvoice.service = dichVu;
			_frmInvoice.amt = tongSo;
			if(this.chbIsFinished.Checked)
			{
				_frmInvoice.postAmt = 0.0;
			}
			else
			{
				_frmInvoice.postAmt = tongSo;
			}
			if(chkBoxPostInfo.Checked)
			{
				_frmInvoice.Guest = "Guest Name : " + cboGuestName.Text;
				_frmInvoice.Soph = cboSoPhong.Text;
			}
			else
			{
				_frmInvoice.Guest = "Guest Name : " + this.cboGuestName.Text;
			}
			if(clsGlobal.g_UserFullName != "")
			{
				_frmInvoice.name = clsGlobal.g_UserFullName;
			}
			else
			{
				_frmInvoice.name = clsGlobal.g_UserName;
			}
			
			lsvSale.Items.Clear();
			
			_frmInvoice.Show();
			
			chbIsFinished.Checked = false;
			chkBoxPostInfo.Checked = false;
			
			//Giai phong cho cac phan lam ve Paid Amount
			this.txtTotal.Text = "0.0";
			lsvSale.Items.Clear();
		}

		private void frmPOSTransaction_Load(object sender, System.EventArgs e)
		{
			DataTable dtItemList = clsStock.GetAllItemForAnDepartment(clsGlobal.g_DepartmentId);
			this.cboName.DataSource = dtItemList;
			this.cboName.DisplayMember = "itemName";
			this.cboName.ValueMember = "itemId";
			if(this.cboName.Items.Count > 0)
			{
				this.cboName.SelectedItem = this.cboName.Items[0];
			}

			DataTable dtShiftType = clsShiftType.GetAllShiftType();
			this.cboLoaiKhach.DataSource = dtShiftType;
			this.cboLoaiKhach.DisplayMember = "shiftName";
			this.cboLoaiKhach.ValueMember = "shiftId";

			DataTable dtDanhSachDichVu = clsService.GetAllServices();
			this.cboLoaiDichVu.DataSource = dtDanhSachDichVu;
			this.cboLoaiDichVu.DisplayMember = "serviceName";
			this.cboLoaiDichVu.ValueMember = "dichVuId_HMS";
			if(this.cboLoaiDichVu.Items.Count > 0)
			{
				this.cboLoaiDichVu.SelectedItem = this.cboLoaiDichVu.Items[0];
			}

			//Load du thong tin hoa don tu co so du lieu len form
			//Du lieu trong bang tblSale
			DataTable dt = clsSale.GetAnItem(this.saleId);
			if(dt.Rows.Count == 0)
			{
				MessageBox.Show("Hóa đơn không tồn tại trong cơ sở dữ liệu. ");
			}
			else
			{
				this.dtpSale.Value = Convert.ToDateTime(dt.Rows[0]["invoiceDate"]);
				
				int i = this.cboLoaiKhach.FindString(Convert.ToString(dt.Rows[0]["shiftName"]));
				this.cboLoaiKhach.SelectedItem = this.cboLoaiKhach.Items[i];

				int serviceId = Convert.ToInt32(dt.Rows[0]["serviceId"]);
				if(serviceId != -1)
				{
					string serviceName = clsService.GetServiceName(serviceId);
					i = this.cboLoaiDichVu.FindString(serviceName);
					this.cboLoaiDichVu.SelectedItem = this.cboLoaiDichVu.Items[i];
				}

				int guestId = Convert.ToInt32(dt.Rows[0]["guestId"]);
				if(guestId != -1)
				{
					this.chkBoxPostInfo.Checked = true;
					
					string guestName = clsSale.GetGuestName(guestId);
					if(guestName ==  "")
					{
						MessageBox.Show("Tên khách không có trong HMS. ");
					}
					else
					{
						i = this.cboGuestName.FindString(guestName);
						if(i >= 0)
						{
							this.cboGuestName.SelectedItem = this.cboGuestName.Items[i];
						}
					}
					
					string roomNumber = Convert.ToString(dt.Rows[0]["roomNumber"]);
					i = this.cboSoPhong.FindString(roomNumber);
					this.cboSoPhong.SelectedItem = this.cboSoPhong.Items[i];
				}

				double total = Convert.ToDouble(dt.Rows[0]["total"]);
				this.txtTotalAmt.Text = total.ToString();

				double billAmount = Convert.ToDouble(dt.Rows[0]["invoiceAmount"]);
				this.txtTotal.Text = billAmount.ToString();

				double roomServiceTax = Convert.ToDouble(dt.Rows[0]["roomServiceTax"]);
				this.txtRoomService.Text = roomServiceTax.ToString();

				this.txtRemark.Text = dt.Rows[0]["remark"].ToString();
			}

			//Load du lieu tu bang tblSaleDetail vao trong listview
			DataTable dtDetail = clsSaleDetail.GetBillDetail(this.saleId);
			this.dtChiTietHoaDonCu = clsSaleDetail.GetBillDetail(this.saleId);
			if(dtDetail.Rows.Count == 0)
			{
				MessageBox.Show("Hóa đơn trống. ");
			}
			else
			{
				for(int i = 0; i < dtDetail.Rows.Count; i++)
				{
					ListViewItem lvi = new ListViewItem(new string[]{dtDetail.Rows[i]["soHoaDonCon"].ToString(), dtDetail.Rows[i]["itemName"].ToString(), 
						dtDetail.Rows[i]["itemId"].ToString(), dtDetail.Rows[i]["qty"].ToString(), 
						dtDetail.Rows[i]["unitName"].ToString(), dtDetail.Rows[i]["unitId"].ToString(), 
						dtDetail.Rows[i]["rate"].ToString(), dtDetail.Rows[i]["amount"].ToString()});
					this.lsvSale.Items.Add(lvi);
				}
			}
	
//			if(clsGlobal.g_DepartmentId == 1)
//			{
//				//cap nhat lai so luong hang hoa trong kho xem nhu chua tung co hoa don nay
//				for(int i = 0; i < this.dtChiTietHoaDonCu.Rows.Count; i++)
//				{
//					int itemId = Convert.ToInt32(this.dtChiTietHoaDonCu.Rows[i]["itemId"]);
//					if(!clsItem.HangPhaTron(itemId))
//					{
//						double qty = Convert.ToDouble(this.dtChiTietHoaDonCu.Rows[i]["qty"]);
//						int unitId = Convert.ToInt32(this.dtChiTietHoaDonCu.Rows[i]["unitId"]);
//						int donViChuanId = clsItem.GetDonViChuanId(itemId);
//						double factor = clsUnitConversion.GetFactor(itemId, donViChuanId, unitId);
//						double soLuongTheoDonViChuan = 0.0;
//						if(factor == -1)
//						{
//							MessageBox.Show("Mặt hàng có id " + itemId + " thiếu đơn vị chuyển đổi. "); 
//						}
//						else
//						{
//							soLuongTheoDonViChuan = qty / factor;	
//						}
//						clsStock.UpdateQuantity(itemId, clsGlobal.g_DepartmentId, soLuongTheoDonViChuan, "+");
//					}
//					else
//					{
//						DataTable danhSachThanhPhan = clsStaple.DanhSachThanhPhan(itemId);
//						double soLuongHangTongHop = Convert.ToDouble(this.dtChiTietHoaDonCu.Rows[i]["qty"]);
//						for(int j = 0; j < danhSachThanhPhan.Rows.Count; j++)
//						{
//							int thanhPhanId = Convert.ToInt32(danhSachThanhPhan.Rows[j]["stapleId"]);
//							double soLuongThanhPhanTrongCongThuc = Convert.ToDouble(danhSachThanhPhan.Rows[j]["qty"]);
//							int donViChuanIdCuaThanhPhan = clsItem.GetDonViChuanId(thanhPhanId);
//							int donViIdCuaThanhPhanTrongCongThuc = Convert.ToInt32(danhSachThanhPhan.Rows[j]["unitId"]);
//							double factor = clsUnitConversion.GetFactor(thanhPhanId, donViChuanIdCuaThanhPhan, donViIdCuaThanhPhanTrongCongThuc);
//							double soLuongThanhPhanTheoDonViChuan = soLuongHangTongHop * soLuongThanhPhanTrongCongThuc / factor;
//							clsStock.UpdateQuantity(thanhPhanId, clsGlobal.g_DepartmentId, soLuongThanhPhanTheoDonViChuan, "+");
//						}
//					}
//				}
//			}

			this.ActiveControl = this.txtSoHoaDonCon;
			this.txtSoHoaDonCon.SelectAll();
		}

		public bool IsDecimal(string theValue)
		{
			try
			{
				Convert.ToDouble(theValue);
				return true;
			} 
			catch 
			{
				return false;
			}
		}

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			bool isLegal = true;
			string strAlert = "";
			
			if(this.cboName.SelectedItem == null)
			{
				isLegal = false;
				strAlert += "Hãy chọn mặt hàng để thêm. ";
			}

			if(!this.IsDecimal(this.txtQty.Text))
			{
				isLegal = false;
				strAlert += "Số lượng phải là một số thực khác 0. ";
			}

			if(this.cboUnit.SelectedItem == null)
			{
				isLegal = false;
				strAlert += "Hãy chọn một đơn vị để thêm. ";
			}

			if(!this.IsDecimal(this.txtRate.Text))
			{
				isLegal = false;
				strAlert += "Giá thành phải là một số thực. ";
			}

			//Kiem tra xem mat hang đa co trong danh muc chua
			for(int i = 0; i < this.lsvSale.Items.Count; i++)
			{
				if(this.lsvSale.Items[i].SubItems[2].Text == this.cboName.SelectedValue.ToString())
				{
					isLegal = false;
					strAlert += "Mặt hàng này đã có trong danh mục. ";
					break;
				}
			}
			//Kiem tra xem mat so luong mat hang duoc chon con du trong kho khong
//			if(this.cboName.SelectedItem != null && this.cboUnit.SelectedItem != null && clsGlobal.g_DepartmentId==1 && Convert.ToInt32(this.cboLoaiDichVu.SelectedValue)!=9)
//			{
//			
//				int itemId = Convert.ToInt32(this.cboName.SelectedValue);
//				double soLuongYeuCauXuat = Convert.ToDouble(this.txtQty.Text);
//				if(!clsItem.HangPhaTron(itemId))
//				{
//					double soLuongConTheoDonViChuan = clsStock.SoLuongHangTrongKho(itemId, clsGlobal.g_DepartmentId);
//					int donViChuanId = clsItem.GetDonViChuanId(Convert.ToInt32(this.cboName.SelectedValue));
//					if(donViChuanId == -1)
//					{
//						isLegal = false;
//						strAlert += "Mặt hàng có id là " + Convert.ToInt32(this.cboName.SelectedValue) + " chưa có đơn vị chuẩn";
//					}
//					int donViDuocChonId = Convert.ToInt32(this.cboUnit.SelectedValue);
//					double tyLeChuyenDoi = clsUnitConversion.GetFactor(Convert.ToInt32(this.cboName.SelectedValue), donViChuanId, donViDuocChonId);
//					if(tyLeChuyenDoi == -1)
//					{
//						isLegal = false;
//						strAlert += "Mặt hàng có id là " + Convert.ToInt32(this.cboName.SelectedValue) + " thiếu đơn vị chuyển đổi. ";
//					}
//					double soLuongConTheoDonViDuocChon = soLuongConTheoDonViChuan * tyLeChuyenDoi;
//					if(soLuongYeuCauXuat > soLuongConTheoDonViDuocChon)
//					{
//						isLegal = false;
//						strAlert += "Số lượng hàng trong kho không còn đủ để bán. ";
//					}
//				}
//				else
//				{
//					DataTable danhSachThanhPhan = clsStaple.DanhSachThanhPhan(itemId);
//					for(int j = 0; j < danhSachThanhPhan.Rows.Count; j++)
//					{
//						int thanhPhanId = Convert.ToInt32(danhSachThanhPhan.Rows[j]["stapleId"]);
//						double soLuongThanhPhanConTheoDonViChuan = clsStock.SoLuongHangTrongKho(thanhPhanId, clsGlobal.g_DepartmentId);
//
//						double soLuongThanhPhanCanBanTheoDonViTrongCongThuc = soLuongYeuCauXuat * Convert.ToDouble(danhSachThanhPhan.Rows[j]["qty"]);
//						int donViChuanIdThanhPhan = clsItem.GetDonViChuanId(thanhPhanId);
//						int donViIdThanhPhanTrongCongThuc = Convert.ToInt32(danhSachThanhPhan.Rows[j]["unitID"]);
//						double factor = clsUnitConversion.GetFactor(thanhPhanId, donViChuanIdThanhPhan, donViIdThanhPhanTrongCongThuc);
//						double soLuongThanhPhanCanBanTheoDonViChuan = soLuongThanhPhanCanBanTheoDonViTrongCongThuc / factor;
//						if(soLuongThanhPhanCanBanTheoDonViChuan > soLuongThanhPhanConTheoDonViChuan)
//						{
//							isLegal = false;
//							strAlert += "Số lượng hàng " + clsItem.GetTenHang(thanhPhanId) + " trong kho không còn đủ để bán.";
//						}
//					}
//				}
//			}

			if(!isLegal)
			{
				MessageBox.Show(strAlert);
			}
			else
			{
				ListViewItem lvi = new ListViewItem(new string[]{this.txtSoHoaDonCon.Text, this.cboName.Text, this.cboName.SelectedValue.ToString(), 
																	this.txtQty.Text, this.cboUnit.Text, this.cboUnit.SelectedValue.ToString(), 
																	this.txtRate.Text, this.txtAmt.Text});
				this.lsvSale.Items.Add(lvi);

				this.TinhToanTongGiaTri();

				this.ActiveControl = this.txtSoHoaDonCon;
				this.txtSoHoaDonCon.SelectAll();
			}
		}

		private void TinhToanTongGiaTri()
		{
			double totalAmount = 0.0;
			for(int i = 0; i < this.lsvSale.Items.Count; i++)
			{
				totalAmount += Convert.ToDouble(this.lsvSale.Items[i].SubItems[7].Text);
			}
			this.txtTotalAmt.Text = totalAmount.ToString();

			if(!this.IsDecimal(this.txtRoomService.Text))
			{
				MessageBox.Show("Các loại phí khác phải là số thực. ");
			}
			else
			{
				double otherFee = Convert.ToDouble(this.txtRoomService.Text);
				double totalFee = totalAmount + otherFee;
				this.txtTotal.Text = totalFee.ToString();
			}
		}

		private void txtQty_TextChanged(object sender, System.EventArgs e)
		{
			if(!this.IsDecimal(this.txtQty.Text))
			{
				MessageBox.Show("Số lượng phải là một số thực khác 0.");
			}
			else
			{
				if(this.IsDecimal(this.txtRate.Text))
				{
					double dbAmt = Convert.ToDouble(this.txtQty.Text)*Convert.ToDouble(this.txtRate.Text);
					this.txtAmt.Text = dbAmt.ToString();
				}
			}
		}

		private void btnUpdate_Click(object sender, System.EventArgs e)
		{
			if(this.lsvSale.FocusedItem == null)
			{
				MessageBox.Show("Hãy chọn một mục hàng để chỉnh sửa. ");
			}
			else
			{
				bool isLegal = true;
				string strAlert = "";
			
				if(this.cboName.SelectedItem == null)
				{
					isLegal = false;
					strAlert += "Hãy chọn mặt hàng để thêm. ";
				}

				if(!this.IsDecimal(this.txtQty.Text))
				{
					isLegal = false;
					strAlert += "Số lượng phải là một số thực khác 0. ";
				}

				if(this.cboUnit.SelectedItem == null)
				{
					isLegal = false;
					strAlert += "Hãy chọn một đơn vị để thêm. ";
				}

				if(!this.IsDecimal(this.txtRate.Text))
				{
					isLegal = false;
					strAlert += "Giá thành phải là một số thực. ";
				}

				//Kiem tra xem mat hang đa co trong danh muc chua
				int selectedIndex = this.lsvSale.FocusedItem.Index;
				for(int i = 0; i < this.lsvSale.Items.Count; i++)
				{
					if(i != selectedIndex && this.lsvSale.Items[i].SubItems[2].Text == this.cboName.SelectedValue.ToString())
					{
						isLegal = false;
						strAlert += "Mặt hàng này trùng với một mục hàng khác. ";
						break;
					}
				}

				//Kiem tra xem mat hang duoc chon con du so luong de ban khong
				if(this.cboName.SelectedItem != null && this.cboUnit.SelectedItem != null && clsGlobal.g_DepartmentId == 1 && Convert.ToInt32(this.cboLoaiDichVu.SelectedValue)!=9)
				{
					int itemId = Convert.ToInt32(this.cboName.SelectedValue);
					double soLuongYeuCauXuat = Convert.ToDouble(this.txtQty.Text);
					if(!clsItem.HangPhaTron(itemId))
					{
						double soLuongConTheoDonViChuan = clsStock.SoLuongHangTrongKho(itemId, clsGlobal.g_DepartmentId);
						int donViChuanId = clsItem.GetDonViChuanId(Convert.ToInt32(this.cboName.SelectedValue));
						if(donViChuanId == -1)
						{
							isLegal = false;
							strAlert += "Mặt hàng có id là " + Convert.ToInt32(this.cboName.SelectedValue) + " chưa có đơn vị chuẩn";
						}
						int donViDuocChonId = Convert.ToInt32(this.cboUnit.SelectedValue);
						double tyLeChuyenDoi = clsUnitConversion.GetFactor(Convert.ToInt32(this.cboName.SelectedValue), donViChuanId, donViDuocChonId);
						if(tyLeChuyenDoi == -1)
						{
							isLegal = false;
							strAlert += "Mặt hàng có id là " + Convert.ToInt32(this.cboName.SelectedValue) + " thiếu đơn vị chuyển đổi. ";
						}
						double soLuongConTheoDonViDuocChon = soLuongConTheoDonViChuan * tyLeChuyenDoi;
						if(soLuongYeuCauXuat > soLuongConTheoDonViDuocChon)
						{
							isLegal = false;
							strAlert += "Số lượng hàng trong kho không còn đủ để bán. ";
						}
					}
					else
					{
						DataTable danhSachThanhPhan = clsStaple.DanhSachThanhPhan(itemId);
						for(int j = 0; j < danhSachThanhPhan.Rows.Count; j++)
						{
							int thanhPhanId = Convert.ToInt32(danhSachThanhPhan.Rows[j]["stapleId"]);
							double soLuongThanhPhanConTheoDonViChuan = clsStock.SoLuongHangTrongKho(thanhPhanId, clsGlobal.g_DepartmentId);

							double soLuongThanhPhanCanBanTheoDonViTrongCongThuc = soLuongYeuCauXuat * Convert.ToDouble(danhSachThanhPhan.Rows[j]["qty"]);
							int donViChuanIdThanhPhan = clsItem.GetDonViChuanId(thanhPhanId);
							int donViIdThanhPhanTrongCongThuc = Convert.ToInt32(danhSachThanhPhan.Rows[j]["unitID"]);
							double factor = clsUnitConversion.GetFactor(thanhPhanId, donViChuanIdThanhPhan, donViIdThanhPhanTrongCongThuc);
							double soLuongThanhPhanCanBanTheoDonViChuan = soLuongThanhPhanCanBanTheoDonViTrongCongThuc / factor;
							if(soLuongThanhPhanCanBanTheoDonViChuan > soLuongThanhPhanConTheoDonViChuan)
							{
								isLegal = false;
								strAlert += "Số lượng hàng " + clsItem.GetTenHang(thanhPhanId) + " trong kho không còn đủ để bán.";
							}
						}
					}
				}

				if(!isLegal)
				{
					MessageBox.Show(strAlert);
				}
				else
				{
					this.lsvSale.FocusedItem.SubItems[0].Text = this.txtSoHoaDonCon.Text;
					this.lsvSale.FocusedItem.SubItems[1].Text = this.cboName.Text;
					this.lsvSale.FocusedItem.SubItems[2].Text = this.cboName.SelectedValue.ToString();
					this.lsvSale.FocusedItem.SubItems[3].Text = this.txtQty.Text;
					this.lsvSale.FocusedItem.SubItems[4].Text = this.cboUnit.Text;
					this.lsvSale.FocusedItem.SubItems[5].Text = this.cboUnit.SelectedValue.ToString();
					this.lsvSale.FocusedItem.SubItems[6].Text = this.txtRate.Text;
					this.lsvSale.FocusedItem.SubItems[7].Text = this.txtAmt.Text;

					this.TinhToanTongGiaTri();
				}
			}
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			bool duDieuKien = true;
			string thongBao = "";
			//Kiem tra cac dieu kien 
//			if(this.chkBoxPostInfo.Checked)
//			{
//				if(cboGuestName.SelectedItem == null)
//				{
//					duDieuKien = false;
//					thongBao += "Hãy chọn tên khách. ";
//				}
//				if(this.cboSoPhong.SelectedItem == null)
//				{
//					duDieuKien=false;
//					thongBao+="Hãy chọn một phòng. ";
//				}
//			}
//
//			if(this.cboLoaiKhach.SelectedItem == null)
//			{
//				duDieuKien = false;
//				thongBao += "Hãy chọn loại khách. ";
//			}
//
//			if(this.lsvSale.Items.Count  == 0)
//			{
//				duDieuKien = false;
//				thongBao += "Không có mặt hàng nào trong danh mục. ";
//			}

			if(!duDieuKien)
			{
				MessageBox.Show(thongBao);
			}
			else
			{
				//cap nhat bang tblSale
				clsSale cs = new clsSale();
				cs.InvoiceDate = this.dtpSale.Text;
				cs.DepartmentId = clsGlobal.g_DepartmentId;
				
				cs.Total = Convert.ToDouble(this.txtTotalAmt.Text);
				cs.InvoiceAmount = Convert.ToDouble(this.txtTotal.Text);
				cs.UserId = clsGlobal.g_UserId;
				if(this.chkBoxPostInfo.Checked)
				{
					cs.GuestId = Convert.ToInt32(this.cboGuestName.SelectedValue);
					cs.RoomNumber = this.cboSoPhong.SelectedValue.ToString();
				}
				else
				{
					cs.GuestId = -1;
					cs.RoomNumber = "-1";
				}
				cs.Remark = this.txtRemark.Text + "";
				cs.IsFinished = this.chbIsFinished.Checked;
				cs.ShiftTypeId = Convert.ToInt32(this.cboLoaiKhach.SelectedValue);
				if(this.chbIsFinished.Checked)
				{
					if(this.rdbVND.Checked)
					{
						cs.PaidAmount = Convert.ToDouble(this.txtTotal.Text);
						cs.PostAmount = 0;
					}
					else
					{
						double tyGia = clsSale.GetTyGiaUSD();
						cs.PaidAmount = Convert.ToDouble(this.txtTotal.Text) * tyGia;
						cs.PostAmount = 0;
					}
				}
				else
				{
					if(this.rdbVND.Checked)
					{
						cs.PaidAmount = 0;
						cs.PostAmount = Convert.ToDouble(this.txtTotal.Text);
					}
					else
					{
						double tyGia = clsSale.GetTyGiaUSD();
						cs.PaidAmount = 0;
						cs.PostAmount = Convert.ToDouble(this.txtTotal.Text) * tyGia;
					}
				}
				
				cs.updateForAnID(this.saleId);

				//neu la khach trong khach san thi phai lap phieu ky
				if(this.chkBoxPostInfo.Checked)
				{
					double sotienan=0;
					double sotienuong = 0;
					double sotienkhac = 0;
					for(int i = 0; i < this.lsvSale.Items.Count; i++)
					{
						int dvid= clsSale.GetDichvuIDByItemID(Convert.ToInt32(this.lsvSale.Items[i].SubItems[2].Text));
						if (dvid == 1)
						{
							sotienan += Convert.ToDouble(this.lsvSale.Items[i].SubItems[7].Text);
						}
						else
						{
							if (dvid == 2)
							{
								sotienuong += Convert.ToDouble(this.lsvSale.Items[i].SubItems[7].Text);
							}
							
							else
							{
								sotienkhac += Convert.ToDouble(this.lsvSale.Items[i].SubItems[7].Text);
							}
						}
					}
					int shdmax = this.saleId;;
					if (sotienan > 0)
					{
						cs.PostVND(shdmax,sotienan,1);
					}
					if (sotienuong > 0)
					{
						cs.PostVND(shdmax,sotienuong,2);
					}

					if (sotienkhac > 0)
					{
						cs.PostVND(shdmax,sotienkhac,7);
					}

				}
				
				
				//cap nhat bang tblSaleDetail
				//Xoa het cac mat hang trong hoa don cu
				clsSaleDetail.Delete(this.saleId);
				//Insert lai vao hoa don moi
				for(int i = 0; i < this.lsvSale.Items.Count; i++)
				{

					clsSaleDetail sd = new clsSaleDetail();
					sd.SaleId = this.saleId;
					sd.SoHoaDonCon = this.lsvSale.Items[i].SubItems[0].Text;
					sd.ItemId = Convert.ToInt32(lsvSale.Items[i].SubItems[2].Text);
					sd.Quantity = Convert.ToInt32(this.lsvSale.Items[i].SubItems[3].Text);
					sd.UnitId = Convert.ToInt32(this.lsvSale.Items[i].SubItems[5].Text);
					sd.Rate = Convert.ToInt32(this.lsvSale.Items[i].SubItems[6].Text);
					sd.Amount = Convert.ToInt32(this.lsvSale.Items[i].SubItems[7].Text);
					sd.Add();
				}

				//cap nhat lai so luong hang hoa trong kho
				for(int i = 0; i < this.lsvSale.Items.Count; i++)
				{
					int itemId = Convert.ToInt32(this.lsvSale.Items[i].SubItems[2].Text);
					if(!clsItem.HangPhaTron(itemId))
					{
						int donViDuocChonId = Convert.ToInt32(this.lsvSale.Items[i].SubItems[5].Text);
						double soLuongBanTheoDonViDuocChon = Convert.ToDouble(this.lsvSale.Items[i].SubItems[3].Text);
						int donViChuanId = clsItem.GetDonViChuanId(itemId);
						double factor = clsUnitConversion.GetFactor(itemId, donViChuanId, donViDuocChonId);
						double soLuongBanTheoDonViChuan = soLuongBanTheoDonViDuocChon / factor;
						clsStock.UpdateQuantity(itemId, clsGlobal.g_DepartmentId, soLuongBanTheoDonViChuan, "-");
					}
					else
					{
						DataTable danhSachThanhPhan = clsStaple.DanhSachThanhPhan(itemId);
						for(int j = 0; j < danhSachThanhPhan.Rows.Count; j++)
						{
							int thanhPhanId = Convert.ToInt32(danhSachThanhPhan.Rows[j]["stapleId"]);
							double soLuongCuaHangTongHop = Convert.ToDouble(this.lsvSale.Items[i].SubItems[3].Text);
							double soLuongThanhPhanTheoDonViTrongCongThuc = soLuongCuaHangTongHop * Convert.ToDouble(danhSachThanhPhan.Rows[j]["qty"]);
							int donViChuanIdCuaThanhPhan = clsItem.GetDonViChuanId(thanhPhanId);
							int donViTrongCongThucCuaThanhPhan = Convert.ToInt32(danhSachThanhPhan.Rows[j]["unitId"]);
							double factor = clsUnitConversion.GetFactor(thanhPhanId, donViChuanIdCuaThanhPhan, donViTrongCongThucCuaThanhPhan);
							double soLuongThanhPhanTheoDonViChuan = soLuongThanhPhanTheoDonViTrongCongThuc / factor;
							clsStock.UpdateQuantity(thanhPhanId, clsGlobal.g_DepartmentId, soLuongThanhPhanTheoDonViChuan, "-");
						}
					}
				}

				//Thong bao lap hoa don thanh cong
				MessageBox.Show("Hóa đơn đã cập nhật thành công!");
				this.Close();
			}
		}

		private void txtQty_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				if(clsGlobal.g_DepartmentId ==  1)
				{
					this.cboUnit.Focus();
				}
				else
				{
					this.btnAdd.Focus();
				}
			}
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			if(this.lsvSale.FocusedItem == null)
			{
				MessageBox.Show("Hãy chọn một mục hàng để xóa. ");
			}
			else
			{
				int selectedIndex = this.lsvSale.FocusedItem.Index;
				this.lsvSale.FocusedItem.Remove();
				if(this.lsvSale.Items.Count > selectedIndex)
				{
					this.lsvSale.Items[selectedIndex].Selected = true;
				}
				else
				{
					if(this.lsvSale.Items.Count > 0)
					{
						this.lsvSale.Items[selectedIndex - 1].Selected = true;
					}
				}
				this.TinhToanTongGiaTri();
			}
		}

		private void cboName_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.cboName.DisplayMember == "itemName" && this.cboName.ValueMember == "itemId" && this.cboName.SelectedItem != null)
			{
				DataTable dtUnit = clsUnitConversion.GetUnitForAnItem(Convert.ToInt32(this.cboName.SelectedValue));
				this.cboUnit.DataSource = dtUnit;
				this.cboUnit.DisplayMember = "unitName";
				this.cboUnit.ValueMember = "unitId";
				int itemId = Convert.ToInt32(this.cboName.SelectedValue.ToString());
				if(this.cboUnit.Items.Count > 0)
				{
					this.cboUnit.SelectedItem = this.cboUnit.Items[0];
					int unitId = Convert.ToInt32(this.cboUnit.SelectedValue.ToString());
					this.txtRate.Text = clsRate.GetRate(itemId, unitId).ToString();
				}

				this.txtMaMatHang.Text = clsItem.GetMaMatHang(itemId);
			}
		}

		#region Create delegate.
		//Khai bao con tro ham.
		public delegate  void Call(String roomNumber, DataTable guestList);

		//Ham ma no thuc su tro den.
		public void CallMethod(String roomNumber, DataTable guestList)
		{
			isGuestLoading = true;
			this.cboSoPhong.Text = roomNumber;
			this.cboGuestName.DataSource = guestList;
			this.cboGuestName.DisplayMember = "GuestName";
			this.cboGuestName.ValueMember = "GuestId";
			isGuestLoading = false;
		}
		#endregion

		private void cboGuestName_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(!isGuestLoading)
			{
				//guestId = Convert.ToInt32(cboGuestName.SelectedValue.ToString());
			}
		}

		private void chkBoxPostInfo_CheckedChanged(object sender, System.EventArgs e)
		{
			if(!this.chkBoxPostInfo.Checked)
			{
				this.cboSoPhong.Enabled = false;
				this.cboGuestName.Enabled = false;
				this.cboName.Focus();
			}
			else
			{
				this.cboSoPhong.Enabled = true;
				this.cboGuestName.Enabled = true;
				DataTable dt = clsRoomDetail.LayDanhSachPhong();
				this.cboSoPhong.DataSource = dt;
				this.cboSoPhong.DisplayMember = "soPhong";
				this.cboSoPhong.ValueMember = "soPhong";

				this.cboSoPhong.Focus();
			}
		}

		private void txtRoomService_TextChanged(object sender, System.EventArgs e)
		{
			if(!this.IsDecimal(this.txtRoomService.Text))
			{
				MessageBox.Show("Các chi phí khác phải là một số thực. ");
				this.txtRoomService.Focus();
			}
			else
			{
				if(this.IsDecimal(this.txtTotalAmt.Text))
				{
					double tongHoaDon = Convert.ToDouble(this.txtRoomService.Text) + Convert.ToDouble(this.txtTotalAmt.Text);
					this.txtTotal.Text = tongHoaDon.ToString();
				}
			}
		}

		private void dtpSale_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.cboLoaiKhach.Focus();
			}
		}

		private void cboName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.txtQty.Text = "";
				this.txtQty.Focus();
			}
		}

		private void cboUnit_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.txtRate.Focus();
			}
		}

		private void txtRate_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.btnAdd.Focus();
			}
		}

		private void txtRate_TextChanged(object sender, System.EventArgs e)
		{
			if(!this.IsDecimal(this.txtRate.Text))
			{
				MessageBox.Show("Giá thành phải là một số thực. ");
			}
			else
			{
				if(this.IsDecimal(this.txtQty.Text))
				{
					double dbAmt = Convert.ToDouble(this.txtQty.Text)*Convert.ToDouble(this.txtRate.Text);
					this.txtAmt.Text = dbAmt.ToString();
				}
			}
		}

		private void lsvSale_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.lsvSale.FocusedItem != null)
			{
				int index = this.cboName.FindString(this.lsvSale.FocusedItem.SubItems[1].Text);
				if(index >= 0)
				{
					this.cboName.SelectedItem = this.cboName.Items[index];
				}

				this.txtQty.Text = this.lsvSale.FocusedItem.SubItems[3].Text;
				
				index = this.cboUnit.FindString(this.lsvSale.FocusedItem.SubItems[4].Text);
				if(index >= 0)
				{
					this.cboUnit.SelectedItem = this.cboUnit.Items[index];
				}
				
				this.txtRate.Text = this.lsvSale.FocusedItem.SubItems[6].Text;

				this.txtAmt.Text = this.lsvSale.FocusedItem.SubItems[7].Text;

				this.txtSoHoaDonCon.Text = this.lsvSale.FocusedItem.SubItems[0].Text;

				int itemId=Convert.ToInt32(this.lsvSale.FocusedItem.SubItems[2].Text);

				int loaiDichVuId=clsItem.GetServiceId(itemId);
				string loaiDichVu=clsService.GetServiceName(loaiDichVuId);
				int loaiDichVuIndex=this.cboLoaiDichVu.FindString(loaiDichVu);
				if(loaiDichVuIndex>=0)
				{
					this.cboLoaiDichVu.SelectedItem=this.cboLoaiDichVu.Items[loaiDichVuIndex];
				}

				this.ActiveControl = this.txtSoHoaDonCon;
				this.txtSoHoaDonCon.SelectAll();
			}
		}

		private void cboUnit_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.cboUnit.DisplayMember == "unitName" && this.cboUnit.ValueMember == "unitId")
			{
				if(this.cboUnit.SelectedItem != null && this.cboName.SelectedItem != null)
				{
					int itemId = Convert.ToInt32(this.cboName.SelectedValue.ToString());
					int unitId = Convert.ToInt32(this.cboUnit.SelectedValue.ToString());
					this.txtRate.Text = clsRate.GetRate(itemId, unitId).ToString();
				}
			}
		}

		private void cboSoPhong_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.cboSoPhong.DisplayMember == "soPhong" && this.cboSoPhong.ValueMember == "soPhong")
			{
				DataTable dt = clsRoomDetail.LayDanhSachKhach(Convert.ToInt32(this.cboSoPhong.SelectedValue));
				this.cboGuestName.DataSource = dt;
				this.cboGuestName.DisplayMember = "khachTen";
				this.cboGuestName.ValueMember = "khachId";

				if(this.cboGuestName.Items.Count > 0)
				{
					this.cboGuestName.SelectedItem = this.cboGuestName.Items[0];
				}
			}
		}

		private void cboLoaiKhach_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.cboLoaiDichVu.Focus();
			}
		}

		private void cboLoaiDichVu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.chkBoxPostInfo.Focus();
			}
		}

		private void cboSoPhong_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.cboGuestName.Focus();
			}
		}

		private void cboGuestName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode ==  Keys.Enter)
			{
				this.cboName.Focus();
			}
		}

		private void chkBoxPostInfo_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.cboName.Focus();
			}
		}

		private void rdbUSD_CheckedChanged(object sender, System.EventArgs e)
		{
			if(this.rdbUSD.Checked)
			{
				double tyGia = clsSale.GetTyGiaUSD();
				double tongTienUSD = Convert.ToDouble(this.txtTotalAmt.Text) / tyGia;
				this.txtTotalAmt.Text = tongTienUSD.ToString();
				double tongHoaDonUSD = Convert.ToDouble(this.txtTotalAmt.Text) / tyGia;
				this.txtTotal.Text = tongHoaDonUSD.ToString();
			}
			else
			{
				double tyGia = clsSale.GetTyGiaUSD();
				double tongTienVND = Convert.ToDouble(this.txtTotalAmt.Text) * tyGia;
				this.txtTotalAmt.Text = tongTienVND.ToString();
				double tongHoaDonVND = Convert.ToDouble(this.txtTotal.Text) * tyGia;
				this.txtTotal.Text = tongHoaDonVND.ToString();
			}
		}

		private void txtTotalAmt_TextChanged(object sender, System.EventArgs e)
		{
			double tongHoaDon = Convert.ToDouble(this.txtTotalAmt.Text) + Convert.ToDouble(this.txtRoomService.Text);
			this.txtTotal.Text = tongHoaDon.ToString();
		}

		private void txtMaMatHang_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode ==  Keys.Enter)
			{
				DataTable dt = clsItem.GetMatHangTuMa(this.txtMaMatHang.Text.Trim());
				if(dt.Rows.Count == 0)
				{
					MessageBox.Show("Ma mat hang khong chinh xac. ");
					this.ActiveControl = this.txtMaMatHang;
					this.txtMaMatHang.SelectAll();
				}
				else
				{
					string tenMatHang = dt.Rows[0]["itemName"].ToString();
					int i = this.cboName.FindString(tenMatHang);
					if(i >= 0)
					{
						this.cboName.SelectedItem = this.cboName.Items[i];
					}
					this.ActiveControl = this.txtQty;
					this.txtQty.SelectAll();
				}
			}
		}

		private void txtSoHoaDonCon_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter){
				this.ActiveControl = this.txtMaMatHang;
				this.txtMaMatHang.SelectAll();
			}
		}	
	}
}
